﻿#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<assert.h>

//memcpy 使⽤和模拟实现

// void * memcpy ( void * destination, const void * source, size_t num );

//int main() {
//	int arr1[] = { 1,2,3,4,5,6,7 };
//	int arr2[20] = { 0 };
//	memcpy(arr2, arr1, 4 * sizeof(int));
//	return 0;
//}

//int main() {
//	int arr1[] = { 1,2,3,4,5,6,7 };   //memcpy不考虑重叠情况，可能会出现错误
//	int arr2[20] = { 0 };
//	memcpy(arr1+2, arr1, 4 * sizeof(int));
//	return 0;
//}

//void* My_memcpy(void* dest, const void* src, size_t num) {
//	void* ret = dest;
//	assert(dest && src);
//	while (num) {
//		*(char*)dest = *(char*)src;
//		dest = (char*)dest + 1;
//		src = (char*)src + 1;
//		num--;
//	}
//	return dest;
//}
//
//int main() {
//	int arr1[] = { 1,2,3,4,5,6,7 };
//	int arr2[20] = { 0 };
//	My_memcpy(arr2, arr1, 4 * sizeof(int));
//	return 0;
//}


//memmove 使⽤和模拟实现

//void* memmove(void* destination, const void* source, size_t num);

void* My_memmove(void* dest, const void* src, size_t num) {
	void* ret = dest;
	assert(dest && src);
	if (src < dest) {
		while (num--) {
			*((char*)dest + num) = *((char*)src + num);
		}
	}
	else {
		while (num--) {
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	return dest;
}

int main() {
	int arr1[] = { 1,2,3,4,5,6,7 };  
	My_memmove(arr1+2, arr1, 4 * sizeof(int));                  
	return 0;
}

//memset 函数的使⽤

//void* memset(void* ptr, int value, size_t num);

//int main() {
//	int arr1[] = { 1,2,3,4,5,6 };      
//	char arr2[] = "hello, bit";
//	memset(arr1, 0, 24);                //数组的内容全部置为0
//	memset(arr2, 'x', 6);				//数组中hello 置为‘x’
//	return 0;
//}

//memcmp 函数的使⽤

//int memcmp(const void* ptr1, const void* ptr2, size_t num);

//int main() {
//	int arr1[] = { 1,2,3,4,5,6,7 };  
//	int arr2[] = { 1,2,3,4,6 };
//	//01 00 00 00   02 00 00 00   03 00 00 00   04 00 00 00   05 00 00 00 
//	//01 00 00 00   02 00 00 00   03 00 00 00   04 00 00 00   06 00 00 00
//	int ret = memcmp(arr1, arr2, 20);
//	printf("%d\n", ret);
//	return 0;
//}
